<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            顺引校验
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">车架号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="carSequenceNo" placeholder="请 录入/扫描车序号" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">位置码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="location" placeholder="请 录入/位置码" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" placeholder="请 录入/扫描物料号" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:5px;"></div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%" onclick="check()">校验</div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%" onclick="doSearch()">查询</div>
        <div style="background-color:white; height: 270px;width: 100%;margin-top:5px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center'>车号</td>
                        <td algin='center'>车序号</td>
                        <td algin='center'>顺序号</td>
                        <td algin='center'>校验标识</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>

        <div style="background-color:white; height: 270px;width: 100%;margin-top:5px;">
            <table class="tableDetails" style='width:100%;' id="tableDetails">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center'>车型代码</td>
                        <!-- <td algin='center'>底盘号</td> -->
                        <td algin='center'>零件号</td>
                        <td algin='center'>顺序号</td>
                        <td algin='center'>校验标识</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtmlDetails">
                </tbody>
            </table>
        </div>
    </div>

    <div>
        <div style="height:50px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    //上次扫描的位置码
    var seq = 0;


    //初始化
    apiready = function () {

        idSelector = "carSequenceNo"
        $("#carSequenceNo").select();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：车序号
            if (idSelector == "carSequenceNo") {

                $("#carSequenceNo").val(ret);

                autoSearch(ret);

                //2:位置码
            } else if (idSelector == "location") {

                $("#location").val(ret);
                locationCheck()
                //3:物料号
            } else if (idSelector == "mrlCode") {
                var location = $("#location").val();
                if (location == "") {
                    api.toast({
                        msg: "请 录入/扫描位置码",
                        duration: 3000
                    });
                    return;
                }

                $("#tm").val(ret);


                //注释 addd luy 20230112
                //替换空格和-字符
                // ret = ret.substr(0, ret.length - 10);
                // ret = ret.replace(/-/g, "");
                // ret = ret.replace(/\s+/g, "");
                $("#mrlCode").val(ret);

                autoCheck(ret);
            }
        });
    }

    //位置码扫描事件
    $("#location").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //车序号
            $("#location").val(sm);

            //校验是否按照顺序扫描位置码
            locationCheck();
        }
    });

    //车序号扫描事件
    $("#carSequenceNo").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //车序号
            $("#carSequenceNo").val(sm);

            //查询
            autoSearch(sm);
        }
    });

    //物料号扫描事件
    $("#mrlCode").on('keypress', function (e) { //#keyword为input文本框
        var location = $("#location").val();
        if (location == "") {
            api.toast({
                msg: "请 录入/扫描位置码",
                duration: 3000
            });
            return;
        }
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#tm").val(sm);

            //注释 addd luy 20230112
            //替换空格和-字符为空格
            // sm = sm.substr(0, sm.length - 10);
            sm = sm.replace(/-/g, "");
            sm = sm.replace(/\s+/g, "");
            $("#mrlCode").val(sm);

            //物料号
            // $("#mrlCode").val(sm.substr(0, sm.length - 10));

            //校验
            autoCheck(sm);
        }
    });

    //自动查询
    function autoSearch(tm) {
        if (tm != null && tm != "" && tm != undefined) {

            doSearch();
        }
    }

    //自动校验
    function autoCheck(tm) {
        if (tm != null && tm != "" && tm != undefined) {
            var carSequenceNo = $("#carSequenceNo").val();

            //车序号已录入，校验
            if (carSequenceNo != null && carSequenceNo != "" && carSequenceNo != undefined) {

                check();
            }
        }
    }

    //查询
    function doSearch() {
        //车序号
        var carSequenceNo = $("#carSequenceNo").val();

        //车序号不能为空
        if (carSequenceNo == null || carSequenceNo == "" || carSequenceNo == undefined) {
            api.toast({
                msg: "请 录入/扫描车序号",
                duration: 3000
            });
            return;
        }

        //条码特殊字符处理
        // carSequenceNo = encodeURIComponent(encodeURIComponent(carSequenceNo));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchDeliveryinfo";
        var data = {
            values: {
                carSequenceNo: carSequenceNo,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    //主表
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        html += "<td name='deliveryNo'>" + list[i].deliveryNo + "</td>";
                        if (list[i].carSequenceNo == carSequenceNo) {
                            html += "<td name='carSequenceNo' style='background-color:yellow;'>" + list[i].carSequenceNo + "</td>";
                        } else {
                            html += "<td name='carSequenceNo'>" + list[i].carSequenceNo + "</td>";
                        }
                        html += "<td name='seqno'>" + list[i].seqno + "</td>";
                        if (list[i].checkState == "Y") {
                            html += "<td name='checkState' style='background-color:green;'>" + list[i].checkState + "</td>";
                        } else {
                            html += "<td name='checkState' style='background-color:red;'>" + list[i].checkState + "</td>";
                        }
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //子表
                    var listDetails = eval(myobj.listDetails);
                    var htmlDetails = "";
                    for (var i = 0; i < listDetails.length; i++) {
                        if (listDetails[i].checkState == "Y") {
                            seq = listDetails[i].seqno
                        }
                        htmlDetails += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        htmlDetails += "<td name='carmodelsCode'>" + listDetails[i].carmodelsCode + "</td>";
                        // htmlDetails += "<td name='vin'>" + listDetails[i].vin + "</td>";
                        htmlDetails += "<td name='mtlCode'>" + listDetails[i].mtlCode + "</td>";
                        htmlDetails += "<td name='seqno'>" + listDetails[i].seqno + "</td>";
                        if (listDetails[i].checkState == "Y") {
                            htmlDetails += "<td name='checkState' style='background-color:green;'>" + listDetails[i].checkState + "</td>";
                        } else {
                            htmlDetails += "<td name='checkState' style='background-color:red;'>" + listDetails[i].checkState + "</td>";
                        }
                        htmlDetails += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtmlDetails"), htmlDetails);

                    $("#location").focus();
                    idSelector = "location";

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    $api.html($api.byId("tbodyHtmlDetails"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //校验
    function check() {
        //车序号
        var carSequenceNo = $("#carSequenceNo").val();
        //位置码
        var location = $("#location").val();
        //物料号
        var mrlCode = $("#mrlCode").val();
        //物料号扫描条码
        var tm = $("#tm").val();

        //车序号非空校验
        if (carSequenceNo == "" || carSequenceNo == null || carSequenceNo == undefined) {
            api.toast({
                msg: '请 录入/扫描车序号',
                duration: 3000
            });
            return;
        }

        //位置码非空校验
        if (location == "" || location == null || location == undefined) {
            api.toast({
                msg: '请 录入/扫描位置码',
                duration: 3000
            });
            return;
        }

        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '请 录入/扫描物料号',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '校验中...',
        });

        //特殊字符处理
        // carSequenceNo = encodeURIComponent(encodeURIComponent(carSequenceNo));
        // mrlCode = encodeURIComponent(encodeURIComponent(mrlCode));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/checkDeliveryinfoMrl";
        var data = {
            values: {
                carSequenceNo: carSequenceNo, //车序号
                appVersion: api.appVersion,
                location: location, //位置码
                mrlCode: mrlCode, // 物料号
                tm: tm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "校验成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#mrlCode").val("");
                    $("#tm").val("");
                    $("#location").val("");
                    //查询
                    doSearch();
                    seq = seq + 1
                    if (seq + 1 == 7) {
                        seq = 0
                        $("#carSequenceNo").val("");
                        $("#carSequenceNo").focus();
                        idSelector = "carSequenceNo";
                    }

                } else if (myobj.code == '1') {

                    api.hideProgress();
                    //弹框提示
                    api.confirm({
                        title: '错误提示',
                        msg: myobj.message,
                        buttons: ['确定', '取消']
                    }, function (ret, err) {
                        var index = ret.buttonIndex;
                        if (index == 1) {

                            $("#mrlCode").val("");
                            $("#tm").val("");
                            idSelector = "mrlCode";
                            $("#mrlCode").select()
                            $("#location").val("");
                            seq = 0
                            doSearch();
                        } else {
                            $("#mrlCode").select();
                        }
                    });
                }
            }
        });
    }


    //校验是否按照顺序扫描
    function locationCheck() {
        //车序号
        var carSequenceNo = $("#carSequenceNo").val();
        //车序号非空校验
        if (carSequenceNo == "" || carSequenceNo == null || carSequenceNo == undefined) {
            api.toast({
                msg: '请 录入/扫描车序号',
                duration: 3000
            });
            return;
        }
        var location = $("#location").val();
        if (seq + 1 != location) {

            //弹框提示
            api.confirm({
                title: '错误提示',
                msg: "请按照顺序扫描位置码",
                buttons: ['确定', '取消']
            }, function (ret, err) {
                var index = ret.buttonIndex;
                if (index == 1) {
                    var un = localStorage.getItem('un');
                    var serverUrl = localStorage.getItem('serverUrl');
                    var url = serverUrl + "/app/checkDeliveryinfoSeq";
                    var data = {
                        values: {
                            carSequenceNo: carSequenceNo, //车序号
                            appVersion: api.appVersion,
                            location: location, //位置码
                            un: un
                        }
                    };
                    api.ajax({
                        url: url,
                        method: 'post',
                        dataType: 'json',
                        timeout: 30,
                        returnAll: false,
                        data: data
                    }, function (ret, err) {
                        if (ret) {
                            var data = JSON.stringify(ret);
                            var myobj = eval("(" + data + ")");
                            //成功
                            if (myobj.code == '0') {
                                $("#location").val("");
                                seq = 0
                                doSearch();

                            } else if (myobj.code == '1') {
                                api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                            }
                        }
                    });
                } else {
                    var un = localStorage.getItem('un');
                    var serverUrl = localStorage.getItem('serverUrl');
                    var url = serverUrl + "/app/checkDeliveryinfoSeq";
                    var data = {
                        values: {
                            carSequenceNo: carSequenceNo, //车序号
                            location: location, //位置码
                            appVersion: api.appVersion,
                            un: un
                        }
                    };
                    api.ajax({
                        url: url,
                        method: 'post',
                        dataType: 'json',
                        timeout: 30,
                        returnAll: false,
                        data: data
                    }, function (ret, err) {
                        if (ret) {
                            var data = JSON.stringify(ret);
                            var myobj = eval("(" + data + ")");
                            //成功
                            if (myobj.code == '0') {
                                $("#location").val("");
                                seq = 0
                                doSearch();

                            } else if (myobj.code == '1') {
                                api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                            }
                        }
                    });
                }
            });

        } else {
            $("#mrlCode").focus();
            idSelector = "mrlCode";

        }

    }



    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>